define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config ) {

            // 流程
            $scope.phases = [
                'process_material',
                'process_modify_qty'
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                // 預設流程狀態
                $scope.phases_index = 0;
            
                 // 自動隱蔽鍵盤
                 element_barcode_no = document.getElementById('input_barcode');
                 makeBarcodeInput(element_barcode_no);
                 element_barcode_no.focus();
            }

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if(index == 0){
                        $scope.material_no = '';
                        $scope.material_product_no = '';
                        $scope.material_item_spec = '';
                        $scope.material_qty = '';
                        $scope.material_actual_qty = '';
                    };
                };
            };

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    if($scope.phases_index == 0){
                        material_info_get(barcode, function(itemList){
                            // 在庫狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length > 0){
                                // 在庫 0
                                if(itemList[0].state == '0'){
                                    $scope.phases_index = 1;
                                    $scope.reel_no = itemList[0].reel_no;
                                    $scope.material_no = itemList[0].material_no;
                                    $scope.material_product_no = itemList[0].product_no;
                                    $scope.material_item_spec = itemList[0].item_spec;
                                    $scope.material_qty = itemList[0].qty;
                                    $scope.material_actual_qty = itemList[0].actual_qty;
                                // 80 報廢
                                } else if(itemList[0].state == '80'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_scrapped'));
                                // 40 已上料
                                } else if (itemList[0].state == '40'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_feeding'));
                                // 60 已上線
                                } else if (itemList[0].state == '60'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_online'));
                                // 不存在
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_reel'));
                                }
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                    };
                };
            };

            // 按下確認按鈕，更新捲料數量
            $scope.confirm = function(){
                if($scope.phases_index == 1){
                    var content = {
                        reel_no: $scope.reel_no,
                        actual_qty: $scope.material_actual_qty
                    };
                    reel_info_set_process(content, function(){
                        $scope.BackToPhase(0);
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                    });
                } else {
                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                };
            };

            // 取得 捲料資料
            function material_info_get(reel_no, callback){
                let content = {
                    reel_no:reel_no,
                }
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 更新 捲料數量
            function reel_info_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_info_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            
            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
